package com.module.prms.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.ResolveUnit;

import com.module.prms.dao.OperationDao;
import com.module.prms.dao.RoleOperationDao;
import com.module.prms.vo.FMenuTree;
import com.module.prms.vo.PrmsOperation;
import com.module.prms.vo.PrmsRoleOperation;




/**
*
* <权限-角色操作并联服务类>
* 
* @author  huhui
* @version  [1.0, July 15, 2011]
* @see  [相关类/方法]
* @since  [二代产品/权限组件]
*/	

public class RoleOperationManager extends
		GenericHibernateManager<PrmsRoleOperation, String> {
	static Logger logger = Logger.getLogger(RoleOperationManager.class
			.getName());
	RoleOperationDao dao = RoleOperationDao.getInstance();
	OperationDao operationDao = OperationDao.getInstance();
	private RoleOperationManager() {
	}

	private static RoleOperationManager manager;

	public synchronized static RoleOperationManager getInstance() {
		if (manager == null)
			manager = new RoleOperationManager();
		return manager;
	}

	/**
	 * <删除> <将角色操作并联信息从数据库删除>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public void del(PrmsRoleOperation obj) throws Exception {
		this.delete(obj);
	}

	/**
	 * <删除> <将角色操作并联信息从数据库删除>
	 * 
	 * @param delhql
	 * @see [类、类#方法、类#成员]
	 */
	public void delbyhql(String delhql) throws Exception {
		this.deleteByQuery(delhql);
	}

	/**
	 * <修改> <修改数据库中的角色操作并联信息>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public void upd(PrmsRoleOperation obj) throws Exception {
		this.update(obj);
	}

	public FMenuTree list(PrmsRoleOperation role) throws Exception {
		Session session = this.getSession();
		FMenuTree menuopt = null;
		try {
			menuopt = dao.getmenuopt(session, role);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return menuopt;
	}

	public FMenuTree listorleopt(String roleid) throws Exception {
		Session session = this.getSession();
		FMenuTree roleopt = null;
		try {
			roleopt = dao.getroleopt(session, roleid);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return roleopt;
	}

	public boolean add(FMenuTree mft,String roleid) throws Exception {
		Session session = this.getSession();
		List optlist = mft.getTree();
		Map result = new HashMap();
		String delhql = "delete from PrmsRoleOperation as obj where obj.croleId='"
				+ roleid + "'";
		boolean isTrue = false;
		Transaction tx = null;
		tx = session.beginTransaction();
		try {
			dao.deleteByQuery(session, delhql);
			PrmsRoleOperation roleoptobj = new PrmsRoleOperation();

			for (int j = 0; j < optlist.size(); j++) {
				roleoptobj = (PrmsRoleOperation) ResolveUnit.resolve(
						ResolveUnit.getJsonStr(optlist.get(j)),
						PrmsRoleOperation.class);

				if (roleoptobj.getCoperationId() != null) {
					isTrue = dao.saveOrUpdate(session, roleoptobj);
				}
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return isTrue;
	}

	public List<String> getOperationByRoleIdAndMenuId(String roleId,String menuId){
		Session session = this.getSession();
		List<String> list= new ArrayList<String>();
		try {
			List<String> listoptids=dao.getOperationByRoleIdAndMenuId(session, roleId, menuId);
			if(listoptids!=null&&listoptids.size()>0){
				for(int i=0;i<listoptids.size();i++){
					PrmsOperation po=operationDao.get(session,listoptids.get(i));
					if(po!=null){
						list.add(po.getCoperationFunction());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeSession();
		}
		return list;
	}
	
	@Override
	public GenericHibernateDao<PrmsRoleOperation, String> getDao() {
		return dao;
	}
    public static void main(String[] args) {
    	RoleOperationManager test = new RoleOperationManager();
    	List<String> list=test.getOperationByRoleIdAndMenuId("ff8080813551db2b013555985908001e", "4028850931a804ab0131a80533080001");
    	for(int i =0 ;i<list.size();i++){
    		System.out.println("====="+list.get(i));
    	}
    }
}
